<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author CHUN Dany <chundany@gmail.com>
 */
class Shop extends Admin_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('mod_shop', 'md');
  }
  
  public function view()
  {
    $uriSegment = 4;
    $perPage = 15;  
    $this->data = $this->md->getShop(uri($uriSegment), $perPage);
    pagination_config(site_url(module_controller('view')), $this->md->getShopNum(), $perPage, $uriSegment); // base_url, total_num, per_page, segment
    
    $this->title  = 'Shop Management';
    $this->action = 'Shop Management';
    $this->btnActtion = 'new';
    $this->lblActtion = 'Add New';
  }
  
  public function add()
  {
    if($this->input->post())
    {
      $this->form_validation->set_rules('name', 'Name', 'trim|xss_clean|required|max_length[50]')
                            ->set_rules('flag', 'Shop ID', 'trim|xss_clean|required|max_length[50]|alpha_dash|is_unique['.table('shop').'.'.field('flag').']')
                            ->set_rules('description', 'Description', 'trim|xss_clean|max_length[255]')
                            ->set_message('is_unique', 'The %s field is alreadied exists.');
        
      if ($this->form_validation->run() === TRUE)
      {  
        $config['upload_path'] = UPLOAD_PATH . 'shop/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '512';
      
        $this->load->library('upload', $config);
    
        if($this->upload->do_upload('image'))
        {
          $image = $this->upload->data();
          $imageName = $image['file_name'];
            
          if($this->md->insert($imageName) > 0)
          {
            resize_image($imageName, 0, 0, UPLOAD_PATH . 'shop/', UPLOAD_PATH . 'shop/');
            resize_image($imageName, 250, 250, UPLOAD_PATH . 'shop/', UPLOAD_PATH . 'shop/250x250/');
            resize_image($imageName, 75, 75, UPLOAD_PATH . 'shop/', UPLOAD_PATH . 'shop/75x75/');

            successMsg('Successfully add new shop!');
            redirect(module_controller('view'));
          }
          else
          {
            delete_file(UPLOAD_PATH . 'shop/' . $imageName);
            errorMsg('Could not add new shop, please try again!');
          }
        }
        else if(!$this->upload->display_errors())
        {
          if(($id = $this->md->insert()) > 0)
          {
            successMsg('Successfully add new shop!');
            redirect(module_controller('view'));
          }
          else
          {
            errorMsg('Could not add new, please try again!');
          }
        }
        else
        {
          errorMsg($this->upload->display_errors());
        }
      }
    }
    
    $this->title  = 'Add New Shop';
    $this->action = 'Add New Shop';
  }
  
  public function edit()
  {
    $id  = $this->uri->segment(4);
    $row = $this->md->getShopById($id);
    
    if($this->input->post())
    {
      $this->form_validation->set_rules('name', 'Name', 'trim|xss_clean|required|max_length[50]')
                            ->set_rules('flag', 'Shop ID', 'trim|xss_clean|required|max_length[50]|alpha_dash|uniqueExcept['.table('shop').'.'.field('flag').'.'.field('id').']')
                            ->set_rules('description', 'Description', 'trim|xss_clean|max_length[255]');
      
      if ($this->form_validation->run() === TRUE)
      {
        $config['upload_path'] = UPLOAD_PATH . 'shop/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '512';
      
        $this->load->library('upload', $config);
    
        if($this->upload->do_upload('image'))
        {       
          $image = $this->upload->data();
          $imageName = $image['file_name'];
          
          if($this->md->update($id, $imageName) > 0)
          {
            resize_image($imageName, 0, 0, UPLOAD_PATH . 'shop/', UPLOAD_PATH . 'shop/');
            resize_image($imageName, 250, 250, UPLOAD_PATH . 'shop/', UPLOAD_PATH . 'shop/250x250/');
            resize_image($imageName, 75, 75, UPLOAD_PATH . 'shop/', UPLOAD_PATH . 'shop/75x75/');
            
            delete_file(UPLOAD_PATH . 'shop/' . $row[field('image')]);
            delete_file(UPLOAD_PATH . 'shop/75x75/' . $row[field('image')]);
            delete_file(UPLOAD_PATH . 'shop/250x250/' . $row[field('image')]);

            successMsg('Successfully edit shop!');
            redirect(module_controller('view'));
          }
          else
          {
            delete_file(UPLOAD_PATH . 'shop/' . $imageName);
            errorMsg('Could not edit shop, please try again!');
          }
        }
        else if(!$this->upload->display_errors())
        {
          if($this->md->update($id) > 0)
          {
            successMsg('Successfully edit shop!');
            redirect(module_controller('view'));
          }
          else
          {
            errorMsg('Could not edit, please try again!');
          }
        }
        else
        {
          errorMsg($this->upload->display_errors());
        }
      }
    }
    $this->row = $row;
      
    $this->title  = 'Edit Shop';
    $this->action = 'Edit Shop';
  }
  
  public function delete()
  {
    $id = $this->uri->segment(4);
    
    if ($this->md->delete($id) > 0)
    {
      successMsg('Successfully delete!');
    }
    else
    {
      errorMsg('Could not delete, please try again!');
    }

    redirect(module_controller('view'));
  }
}